package com.zprmb.udzt;

import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.adapter.CouponAdapter;
import com.zprmb.udzt.adapter.CouponAdapter.OnDialogListener;
import com.zprmb.udzt.bean.Mycoupon;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;
/**
 * 我的优惠券
 * @author shenshao
 *
 */
public class MyCoupon extends BaseActivity implements OnRefreshListener2<ListView>,OnClickListener{
	private PullToRefreshListView lv_product_fragment;
	private int page=1;
	private ListView actualListView;
	private ArrayList<Mycoupon> list = new ArrayList<Mycoupon>();
	private ArrayList<Mycoupon> list2 = new ArrayList<Mycoupon>();
	private ImageView img_back;
	private Dialog dialog;
	private CouponAdapter adapter;
	private RelativeLayout rl_back;
	private ImageView img_none;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_coupon);
		 BbtApplication.getInstance().addActvity(this);
		lv_product_fragment = (PullToRefreshListView)findViewById(R.id.lv);
		img_back=(ImageView)findViewById(R.id.img_back);
		img_none=(ImageView)findViewById(R.id.iv_v4_nonetwork);
		img_back.setOnClickListener(this);
		actualListView = lv_product_fragment.getRefreshableView();
		registerForContextMenu(actualListView);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		rl_back.setOnClickListener(this);
		lv_product_fragment.setOnRefreshListener(this);
		lv_product_fragment.setMode(Mode.BOTH);
		initData();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(getIntent().getIntExtra("flag", 0)==1){
			Intent intent=new Intent(MyCoupon.this,MainActivity.class);
			startActivity(intent);
		}
		return super.onKeyDown(keyCode, event);
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		
	}
	private void initData() {
		// TODO Auto-generated method stub
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("coupon", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					
					Gson gson=new Gson();
					JSONObject json=jsonObject.optJSONObject("data");
					if (!jsonObject.optString("data").equals("null")) {
						list=gson.fromJson(json.optString("raisingRatesList"), new TypeToken<ArrayList<Mycoupon>>() {
							}.getType());
						
					}
					list2.addAll(list);
					if(list.size()>0){
						lv_product_fragment.setVisibility(View.VISIBLE);
						img_none.setVisibility(View.GONE);
					}else{
						lv_product_fragment.setVisibility(View.GONE);
						img_none.setVisibility(View.VISIBLE);
					}
					adapter=new CouponAdapter(list2, MyCoupon.this);
					lv_product_fragment.setAdapter(adapter);
					adapter.notifyDataSetChanged();
					adapter.setOnDialopgListener(new OnDialogListener() {
						
						@Override
						public void refresh() {
							// TODO Auto-generated method stub
							handler.sendEmptyMessage(1);
						}
					});
				actualListView.setSelection(list2.size()-10);
				if(list2.size()<1){
					lv_product_fragment.setVisibility(View.GONE);
					img_none.setVisibility(View.VISIBLE);
			}else{
						lv_product_fragment.setVisibility(View.VISIBLE);
						img_none.setVisibility(View.GONE);
					}
				
				}else {
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(MyCoupon.this,LoginActivity.class);
						startActivity(intent);
					}else{
						Toast.makeText(MyCoupon.this, jsonObject.optString("comment"), 2000).show();
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(0);
		volleryUtil.setShow(true);
		volleryUtil.execute(MyCoupon.this, Constant.URL+"/api/v5/user/raisingratesList1?pageNo="+page+"&pageSize=10", "");
		
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			list.clear();
			if(msg.what==1){
				page=1;
				list2.clear();
				initData();
			}else{
				page+=1;
				initData();	
			}
//			Toast.makeText(My_coupon.this, "刷新成功", 2000).show();
			lv_product_fragment.onRefreshComplete();
		};
	};
	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}
	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.img_back:
			if(getIntent().getIntExtra("flag", 0)==1){
				Intent intent=new Intent(MyCoupon.this,MainActivity.class);
				startActivity(intent);
			}
			finish();
			break;
		case R.id.rl_back:
			if(getIntent().getIntExtra("flag", 0)==1){
				Intent intent=new Intent(MyCoupon.this,MainActivity.class);
				startActivity(intent);
			}
			finish();
			break;
		}
		
	}

}
